// ActionScript file
import Modules.Bill;
import Modules.Clients;
import Modules.HomePage;
import Modules.Inventory;
import Modules.Login;
import Modules.Services;

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ModuleEvent;
import mx.modules.IModuleInfo;
import mx.modules.ModuleManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.EnvironmentVariables;
import org.flexlancer.enums.ModuleNames;
import org.flexlancer.events.CustomEvent;

private var moduleinfo:IModuleInfo;

private function OnLoad():void{
	try{
		EnvironmentVariables.Inits();
		this.LoadModule(ModuleNames.LOGIN);
		if ( ! this.UpdateSession() ){
			Alert.show("Please contact to your system administrator at zu.bair@hotmail.com","Trial expried.",Alert.OK,this,OnTrialOk);
		}
	}catch(err:Error){
		Logger.LogE(err);	
	}
}

private function OnTrialOk(evt:CloseEvent):void{
	try{
		//Application.application.close();
	}catch(err:Error){
		Logger.LogE(err);	
	}
}

private function UpdateSession():Boolean{
	try{
		var sessionFile:File = File.applicationStorageDirectory.resolvePath("appsettings.xml");
 		var fs:FileStream = new FileStream();
        var strData:String = "";
        var xmlData:XML;

		if ( sessionFile.exists ){
	        fs.open(sessionFile, FileMode.READ);
	        if ( sessionFile.size > fs.position )
	        	strData = fs.readUTF();
	        fs.close();
		}
        
        if ( strData == "" ){
        	strData = "<app><count>30</count><lastdate>"+(new Date()).getTime()+"</lastdate></app>";
        }
        
       	xmlData = new XML(strData);
       	
        var count:Number = Number(xmlData.count.toString());
        var time:Number = Number(xmlData.lastdate.toString());
        var readTime:Date = new Date();
        
        readTime.setTime(time);
        
        var dtNow:Date = new Date();
        if ( dtNow.date != readTime.date ){
        	count --;
        }
        
        xmlData.count = count.toString();
        xmlData.lastdate = dtNow.getTime();
        fs.open(sessionFile,FileMode.WRITE);
        fs.writeUTF(xmlData.toXMLString());
        fs.close();
        
        if ( count > 0 ) return true;
  	}catch(err:Error){
		Logger.LogE(err);	
	}
	return false;
}
private function LoadModule(parmName:String):void{
	try{
		this.vboxModuleContainer.removeAllChildren();
		switch(parmName.toUpperCase()){
			case ModuleNames.LOGIN:
				this.moduleinfo = ModuleManager.getModule("./Modules/Login.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnLoginReady);
				this.moduleinfo.load();
			break;
			
			case ModuleNames.HOMEPAGE:
				this.moduleinfo = ModuleManager.getModule("./Modules/Homepage.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnHomePageReady);
				this.moduleinfo.load();
			break;

			case ModuleNames.CLIENTS:
				this.moduleinfo = ModuleManager.getModule("./Modules/Clients.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnClientsReady);
				this.moduleinfo.load();
			break;

			case ModuleNames.SERVICES:
				this.moduleinfo = ModuleManager.getModule("./Modules/Services.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnServicesReady);
				this.moduleinfo.load();
			break;

			case ModuleNames.INVENTORY:
				this.moduleinfo = ModuleManager.getModule("./Modules/Inventory.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnInventoryReady);
				this.moduleinfo.load();
			break;

			case ModuleNames.BILL:
				this.moduleinfo = ModuleManager.getModule("./Modules/Bill.swf");
				this.moduleinfo.addEventListener(ModuleEvent.READY, OnBillReady);
				this.moduleinfo.load();
			break;
			default:
			break;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function HomePageClicked():void{
	try{
		this.LoadModule(ModuleNames.HOMEPAGE);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ServicesClicked():void{
	try{
		this.LoadModule(ModuleNames.SERVICES);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ClientsClicked():void{
	try{
		this.LoadModule(ModuleNames.CLIENTS);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function BillClicked():void{
	try{
		this.LoadModule(ModuleNames.BILL);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function InventoryClicked():void{
	try{
		this.LoadModule(ModuleNames.INVENTORY);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnBillReady(evt:ModuleEvent):void{
	try{
		var billObject:Bill = this.moduleinfo.factory.create() as Bill;
		this.vboxModuleContainer.addChild(billObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnInventoryReady(evt:ModuleEvent):void{
	try{
		var inventoryObject:Inventory = this.moduleinfo.factory.create() as Inventory;
		this.vboxModuleContainer.addChild(inventoryObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnServicesReady(evt:ModuleEvent):void{
	try{
		var servicesObject:Services = this.moduleinfo.factory.create() as Services;
		this.vboxModuleContainer.addChild(servicesObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnHomePageReady(evt:ModuleEvent):void{
	try{
		var homepageObject:HomePage = this.moduleinfo.factory.create() as HomePage;
		this.vboxModuleContainer.addChild(homepageObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnClientsReady(evt:ModuleEvent):void{
	try{
		var clientsObject:Clients = this.moduleinfo.factory.create() as Clients;
		this.vboxModuleContainer.addChild(clientsObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnLoginReady(evt:ModuleEvent):void{
	try{
		var loginObject:Login = this.moduleinfo.factory.create() as Login;
		loginObject.addEventListener(CustomEvent.LOGIN_SUCCEED,OnLoginSucceed);
		this.vboxModuleContainer.addChild(loginObject);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnLoginSucceed(evt:CustomEvent):void{
	try{
		this.currentState="";
		this.LoadModule(ModuleNames.HOMEPAGE);
	}catch(err:Error){
		Logger.LogE(err);		
	}
}